package com.his.struts.dao.foods;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;

import com.his.struts.dao.cook.CookImp;
import com.his.struts.form.CookForm;
import com.his.struts.form.FoodForm;
import com.his.struts.form.FoodtypeForm;
import com.his.struts.util.DbUtil;

/**
 * 实现持久层“食物”接口类
 * 
 * @author master
 * 
 */
public class FoodsImp implements FoodsDao {
	Logger log = Logger.getLogger(FoodsImp.class);
	DbUtil db;
	ResultSet rs = null;

	/**
	 * 添加食品
	 * @param foodForm
	 * @return boolean
	 */
	public boolean addFood(FoodForm foodForm) {
		db = new DbUtil();
		Boolean ok = false;
		String sql = "insert into foodhealth (fname,ftype,fimguil,fintroduce,"
				+ "fefficacy,fclew,fnutrition,fcookbook) "
				+ "values(?,?,?,?,?,?,?,?)";
		ArrayList list = new ArrayList();
		list.add(foodForm.getFname());
		list.add(foodForm.getFtype());
		list.add(foodForm.getFimguil());
		list.add(foodForm.getFintroduce());
		list.add(foodForm.getFefficacy());
		list.add(foodForm.getFclew());
		list.add(foodForm.getFnutrition());
		list.add(foodForm.getFcookbook());
		int line = db.executeUpdate(sql, list);
		if (line != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	
	/**
	 * 更新食物图片
	 * @param foodForm
	 * @return boolean
	 */
	public boolean updateFimguil(FoodForm foodForm) {
		db = new DbUtil();
		Boolean ok = false;
		String sql = "update foodhealth set fimguil=? where fid =?";
		ArrayList list = new ArrayList();
		list.add(foodForm.getFimguil());
		list.add(foodForm.getFid());
		int line = db.executeUpdate(sql, list);
		if (line != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	
	/**
	 * 更新食物信息
	 * @param foodForm
	 * @return boolean
	 */
	public boolean updateFoodInfo(FoodForm foodForm) {
		db = new DbUtil();
		Boolean ok = false;
		String sql = "update foodhealth set fintroduce= ?, fclew = ?," +
				" fcookbook=?, fefficacy=?, fnutrition=? where fid =?";
		ArrayList list = new ArrayList();
		list.add(foodForm.getFintroduce());
		list.add(foodForm.getFclew());
		list.add(foodForm.getFcookbook());
		list.add(foodForm.getFefficacy());
		list.add(foodForm.getFnutrition());
		list.add(foodForm.getFid());
		int line = db.executeUpdate(sql, list);
		if (line != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 获得下拉类型
	 * @param stype
	 * @return ArrayList<FoodtypeForm>
	 */
	public ArrayList<FoodtypeForm> getTypelist(String stype) {
		db = new DbUtil();
		ArrayList<FoodtypeForm> typeList = new ArrayList<FoodtypeForm>();
		String sql = "select * from foodtype where  styname = ?";
		ArrayList list = new ArrayList();
		list.add(stype);
		rs = db.executeQuery(sql, list);
		try {
			while (rs.next()) {
				FoodtypeForm typeForm = new FoodtypeForm();
				typeForm.setTpid(rs.getInt("tpid"));
				typeForm.setStyname(rs.getString("styname"));
				typeForm.setTpename(rs.getString("tpename"));
				typeForm.setTpcname(rs.getString("tpcname"));
				typeList.add(typeForm);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return typeList;
	}

	/**
	 * 获得当前食物对象
	 * @param fid
	 * @return FoodForm
	 */
	public FoodForm getFood(int fid) {
		db = new DbUtil();
		FoodForm foodForm = new FoodForm();
		String sql = "select fid,fname,ftype,fimguil,fintroduce,fefficacy,fclew,fnutrition,fcookbook,styname,tpcname" +
				" from foodhealth left join foodtype on tpid = ftype where fid = ?";
		ArrayList list = new ArrayList();
		list.add(fid);
		rs = db.executeQuery(sql, list);
		try {
			while (rs.next()) {
				foodForm.setFid(rs.getInt("fid"));
				foodForm.setFname(rs.getString("fname"));
				foodForm.setFtype(rs.getInt("ftype"));
				foodForm.setFimguil(rs.getString("fimguil"));
				foodForm.setFintroduce(rs.getString("fintroduce"));
				foodForm.setFefficacy(rs.getString("fefficacy"));
				foodForm.setFclew(rs.getString("fclew"));
				foodForm.setFnutrition(rs.getString("fnutrition"));
				foodForm.setFcookbook(rs.getString("fcookbook"));
				foodForm.setFtypecname(rs.getString("styname"));
				foodForm.setTpcname(rs.getString("tpcname"));
			}
			;
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return foodForm;
	}

	/**
	 * 获得相关食物
	 * @param fid
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getRelFoods(int fid) {
		db = new DbUtil();
		ArrayList<String> foodList = new ArrayList<String>();
		String sql = "select fname from foodhealth where ftype = (select ftype from foodhealth where fid =?)";
		ArrayList list = new ArrayList();
		list.add(fid);
		rs = db.executeQuery(sql, list);
		try {
			while (rs.next()) {
				String relfood = rs.getString(1);
				foodList.add(relfood);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return foodList;
	}

	/**
	 * 获得食物列表
	 * @param ftype
	 * @return ArrayList<FoodForm>
	 */
	public ArrayList<FoodForm> getFoodList(int ftype) {
		db = new DbUtil();
		ArrayList<FoodForm> foodList = new ArrayList<FoodForm>();
		String sql = "select * from foodhealth where ftype =? ";
		ArrayList list = new ArrayList();
		list.add(ftype);
		rs = db.executeQuery(sql, list);
		try {
			while (rs.next()) {
				FoodForm foodForm = new FoodForm();
				foodForm.setFid(rs.getInt("fid"));
				foodForm.setFname(rs.getString("fname"));
				foodForm.setFtype(rs.getInt("ftype"));
				foodForm.setFimguil(rs.getString("fimguil"));
				foodForm.setFintroduce(rs.getString("fintroduce"));
				foodForm.setFefficacy(rs.getString("fefficacy"));
				foodForm.setFclew(rs.getString("fclew"));
				foodForm.setFnutrition(rs.getString("fnutrition"));
				foodForm.setFcookbook(rs.getString("fcookbook"));
				foodList.add(foodForm);
			}
			;
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return foodList;
	}

	/**
	 * 获得后台显示的所有食物
	 * @param suptype
	 * @return ArrayList<FoodForm>
	 */
	public ArrayList<FoodForm> getAllFood(ResultSet rs) {
		db = new DbUtil();
		ArrayList<FoodForm> foodList = new ArrayList<FoodForm>();
		
		try {
			while (rs.next()) {
				FoodForm foodForm = new FoodForm();
				foodForm.setTpcname(rs.getString("tpcname"));
				foodForm.setFid(rs.getInt("fid"));
				foodForm.setFname(rs.getString("fname"));
				foodForm.setFimguil(rs.getString("fimguil"));
				foodForm.setFtypecname(rs.getString("styname"));
				foodList.add(foodForm);
			}
			;
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return foodList;
	}

	/**
	 * 删除一个食物对象
	 * @param fid
	 * @return boolean
	 */
	public boolean deleteFood(int fid) {
		db = new DbUtil();
		boolean ok = false;
		String sql = "delete from foodhealth where fid =?";
		ArrayList list = new ArrayList();
		list.add(fid);
		int line = db.executeUpdate(sql, list);
		if (line != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}


	/**
	 * 添加食物一个类型
	 * @param foodtypeForm
	 * @return boolean
	 */
	public boolean addType(FoodtypeForm foodtypeForm) {
		db = new DbUtil();
		Boolean ok = false;
		String sql = "insert into foodtype (styname,tpcname) values(?,?)";
		ArrayList list = new ArrayList();
		list.add(foodtypeForm.getStyname());
		list.add(foodtypeForm.getTpcname());
		int line = db.executeUpdate(sql, list);
		if (line != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}


}
